package com.menhai.personmanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.FollowupRecord;
import com.menhai.personmanagement.entity.FollowupRecordCondition;
import com.menhai.personmanagement.entity.MbReportCard;
import com.menhai.personmanagement.entity.vo.MbReportCardVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 随访记录表 Mapper 接口
 * </p>
 *
 * @author shikun
 * @since 2021-11-25
 */
@Mapper
public interface FollowupRecordMapper extends BaseMapper<FollowupRecord> {

    /**
     * insert
     *
     * @param followupRecord
     * @return int
     */
    public int insert(FollowupRecord followupRecord);

    /**
     * update
     *
     * @param followupRecord
     * @return int
     */
    public int update(FollowupRecord followupRecord);

    /**
     * delete
     *
     * @param id
     * @return int
     */
    public int delete(String id);

    /**
     * getOneByCondition
     *
     * @param followupRecord
     * @return FollowupRecord
     */
    public FollowupRecord getOneByCondition(FollowupRecord followupRecord);


    /**
     * getListByCondition
     *
     * @param followupRecordCondition
     * @return List<Map < String, Object>>
     */
    public List<Map<String, Object>> getListByCondition(FollowupRecordCondition followupRecordCondition);


    /**
     * getCountByCondition
     *
     * @param
     * @return int
     */
    public int getCountByCondition(FollowupRecord followupRecord);


    IPage<FollowupRecordCondition> getListByConditionByPage(@Param("page") Page<FollowupRecordCondition> page, @Param("followupRecordCondition") FollowupRecordCondition followupRecordCondition, @Param("hosnumarray") List hosnumarray);

    public List gecunrenshu(@Param("mbtype") String mbtype, @Param("userid") String userid);

    public List<Map<String, Object>> getOneById(FollowupRecordCondition followupRecordCondition);

    public List<Map<String, Object>> getCardNameListPlanByCondition(@Param("patientName") String patientName, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("shstatus") String shstatus, @Param("sbstatus") String sbstatus, @Param("startDate") String startDate, @Param("endDate") String endDate, @Param("doctorId") String doctorId);

    public List<MbReportCard> getCardNameListNoPlanByCondition(@Param("patientName") String patientName, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("shstatus") String shstatus, @Param("sbstatus") String sbstatus, @Param("startDate") String startDate, @Param("endDate") String endDate, @Param("doctorId") String doctorId);

    public List<MbReportCard> getCardNameListByCondition(@Param("patientName") String patientName, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("shstatus") String shstatus, @Param("sbstatus") String sbstatus, @Param("startDate") String startDate, @Param("endDate") String endDate);

    public List<Map<String, Object>> getPopulationNameListByCondition(@Param("patientName") String patientName, @Param("hosnum") String hosnum);

    /**
     * @param hosnum
     * @param mbType
     * @describe 药品统计
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.MbReportCardVo>
     * @Date 2022/2/9 15:12
     * @author shikun
     */
    public List<MbReportCardVo> drugstatistics(@Param("hosnum") String hosnum, @Param("mbType") String mbType);

    /**
     * 查询随访提醒
     *
     * @param personName
     * @return
     */
    public IPage<List<Map<String, Object>>> selectSFTX(@Param("page") Page<FollowupRecordCondition> page, @Param("personName") String personName, @Param("mbtype") String mbtype, @Param("hosnum") String hosnum);

    /**
     * 三天随访提醒
     *
     * @param planTime
     * @param hosnum
     * @param mbType
     * @return
     */
    public List<Map<String, Object>> getTXList(@Param("planTime") String planTime, @Param("hosnum") String hosnum,
                                               @Param("mbType") String mbType, @Param("userId") String userId, @Param("hosnumStr") String hosnumStr, @Param("hosnumArray") String[] hosnumArray);


    @Select("select count(*) from mb_population p where (p.superiorCode like '${hosnum}%' and p.superiorCode in (${hosnumArray}) ) and p.chronic like '%${mbType}%' and DATE_FORMAT(p.createdate,'%Y')<=#{queryYear} and p.isAvailable=1")
    public int getHzrs(@Param("queryYear") String queryYear, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("hosnumArray") String hosnumArray);

    @Select("select count(DISTINCT populationid) from mb_report_card r where (r.hosnum like '${hosnum}%' and r.hosnum in (${hosnumArray})  ) and r.mbType=#{mbType} and DATE_FORMAT(r.addTime,'%Y')<=#{queryYear} and r.isDelete=1 and r.populationid in (" +
            "select p.id from mb_population p where (p.superiorCode like '${hosnum}%' and p.superiorCode in (${hosnumArray})) and p.chronic like '%${mbType}%' and DATE_FORMAT(p.createdate,'%Y')<=#{queryYear} and p.isAvailable=1)")
    public int getDjrs(@Param("queryYear") String queryYear, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("hosnumArray") String hosnumArray);

    @Select("select count(*) from (" +
            "select personNo from mb_followup_record r where DATE_FORMAT(r.followupDate,'%Y')= #{queryYear} and  r.personNo in (" +
            "select DISTINCT populationid from mb_report_card r where (r.hosnum like '${hosnum}%' and r.hosnum in (${hosnumArray})  ) and r.mbType=#{mbType} and DATE_FORMAT(r.addTime,'%Y')<=#{queryYear} and r.isDelete=1 and r.populationid in (" +
            "select p.id from mb_population p where (p.superiorCode like '${hosnum}%' and p.superiorCode in (${hosnumArray}))  and p.chronic like '%${mbType}%' and DATE_FORMAT(p.createdate,'%Y')<=#{queryYear} and p.isAvailable=1)) and r.mbtype=#{mbType} " +
            "GROUP BY personNo having count(id)>=5) t")
    public int getGfglrs(@Param("queryYear") String queryYear, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("hosnumArray") String hosnumArray);

    @Select("select count(*) from (" +
            "select personNo from mb_followup_record r where DATE_FORMAT(r.followupDate,'%Y')= #{queryYear} and (r.yyqk_ywmc1!=''||r.yyqk_ywmc2!=''||r.yyqk_ywmc3!=''||r.yyqk_qtyw!='') and  r.personNo in (" +
            "select DISTINCT populationid from mb_report_card r where  (r.hosnum like '${hosnum}%' and r.hosnum in (${hosnumArray})  )  and r.mbType=#{mbType} and DATE_FORMAT(r.addTime,'%Y')<=#{queryYear} and r.isDelete=1 and r.populationid in (" +
            "select p.id from mb_population p where  (p.superiorCode like '${hosnum}%' and p.superiorCode in (${hosnumArray}))  and p.chronic like '%${mbType}%' and DATE_FORMAT(p.createdate,'%Y')<=#{queryYear} and p.isAvailable=1)) and r.mbtype=#{mbType} " +
            "GROUP BY personNo) t")
    public int getFyrs(@Param("queryYear") String queryYear, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("hosnumArray") String hosnumArray);

    @Select("select count(DISTINCT populationid) from mb_report_card r where (r.hosnum like '${hosnum}%' and r.hosnum in (${hosnumArray})  )   and r.mbType=#{mbType} and DATE_FORMAT(r.addTime,'%Y')<=#{queryYear} and r.isDelete=1 and r.populationid in (" +
            "select p.id from mb_population p where  (p.superiorCode like '${hosnum}%' and p.superiorCode in (${hosnumArray}))  and p.chronic like '%${mbType}%' and p.chronic like '%,%' and DATE_FORMAT(p.createdate,'%Y')<=#{queryYear} and p.isAvailable=1)")
    public int getBfzrs(@Param("queryYear") String queryYear, @Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("hosnumArray") String hosnumArray);


    /**
     * 规范管理人员规范管理人数
     *
     * @param mbType
     * @param hosnum
     * @param year
     * @return
     */
    public int chronicPatientGroupsStatisticsGFGL(@Param("mbType") String mbType, @Param("hosnum") String hosnum, @Param("year") String year);

    @Select("select l.codeName hosname,a.hosnum,ifnull(a.gxynum,0) gxynum,ifnull(a.tnbnum,0) tnbnum,ifnull(a.gxbnum,0) gxbnum,ifnull(a.zlnum,0) zlnum,ifnull(a.jsbnum,0) jsbnum,ifnull(a.othernum,0) othernum,ifnull(a.num,0) num from codelist l left join( select c.hosnum,sum(IF(c.mbType='高血压',1,0)) gxynum,sum(IF(c.mbType='糖尿病',1,0)) tnbnum,sum(IF(c.mbType='冠心病',1,0)) gxbnum,sum(IF(c.mbType='肿瘤',1,0)) zlnum,sum(IF(c.mbType='精神病',1,0)) jsbnum,sum(IF(c.mbType!='高血压'&&c.mbType!='糖尿病'&&c.mbType!='冠心病'&&c.mbType!='肿瘤'&&c.mbType!='精神病',1,0)) othernum,count(c.bkbh) num from  mb_report_card c where  c.isDelete=1 and DATE_FORMAT(c.addTime,'%Y')=#{queryYear} and c.hosnum like '${hosnum}%' GROUP BY c.hosnum) a on l.codeValue=a.hosnum where l.kindCode='YYMC' and l.codeValue like '${hosnum}%' and LENGTH(l.codeValue)=8 order by l.codeValue asc")
    public List<Map<String, Object>> getMbflrqtjbByYear(@Param("queryYear") String queryYear, @Param("hosnum") String hosnum);

    public List<Map<String, Object>> getMbsfjhtjbByDay(@Param("queryDay") String queryDay, @Param("hosnum") String hosnum, @Param("userid") String userid);

    public List<Map<String, Object>> getMbsfgztjbByDay(@Param("queryDay") String queryDay, @Param("hosnum") String hosnum, @Param("userid") String userid);

    public List<Map<String, Object>> getMbrgzltjbByDay(@Param("queryDay") String queryDay, @Param("hosnum") String hosnum, @Param("userid") String userid);

    public List<Map<String, Object>> getGzlist(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("hosnum") String hosnum, @Param("userid") String userid);
    public List<Map<String, Object>> getGZLTJList(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("hosnum") String hosnum);

    public List<Map<String, Object>> getTZXXList(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("hosnum") String hosnum);
    public List<Map<String, Object>> getZFBZList(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("hosnum") String hosnum);
    public List<Map<String, Object>> getFXZSList(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("hosnum") String hosnum);

    /**
     * 获取慢病分类关联
     *
     * @author: 高云雷
     * @createTime: 2023/8/17 8:59
     * @param: []
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     **/
	@Select("select * from mb_classification_info")
    public List<Map<String, Object>> getMbClassificationInfoList();

    public List<Map<String, Object>> getTZFX(@Param("beginDate") String beginDate, @Param("endDate") String endDate, @Param("hosnum") String hosnum, @Param("uid") String uid,@Param("sql_mb") String sql_mb,@Param("sql_select_mb") String sql_select_mb);

    public List<Map<String, Object>> getMbydgzltjbByYear(@Param("queryYear") String queryYear, @Param("hosnum") String hosnum, @Param("userid") String userid);

    /**
     * 左侧树
     *
     * @param hosnum
     * @return
     */
    public List<Map<String, Object>> geteCunRenShu(@Param("hosnum") String hosnum);
}
